import config from "../common/config";
import store from "/store/index.js";

const request = (url = "", date = {}, type = "GET") => {
  const headerObj = {
    Authorization: store.state.token
  };
  return new Promise((resolve, reject) => {
    uni.request({
      url: config.baseUrl + url,
      method: type,
      data: {
        ...date,
        user_id: store.state.userInfo.user_id,
        dept_id: store.state.userInfo.dept_id,
        comp_no: store.state.userInfo.comp_no,
      },
      header: headerObj,
      dataType: "json",
      success: (res) => {
        const code = res.data.status;
        resolve(res.data);
        if (code != 200) {
          return uni.showToast({
            title: res.data.msg,
            duration: 2000,
            icon: 'none'
          })
        }
      },
      fail: (error) => {
        reject(error);
      },
      complete: () => {
      },
    });
  });
};
export const get = (url, data) => {
  return request(url, data);
};

export const post = (url, data) => {
  return request(url, data, "POST");
};
